package com.gem.controller.mobile;

import com.gem.domain.CDailySpending;
import com.gem.domain.User;
import com.gem.service.CDailySpendingService;
import com.gem.service.UserService;
import com.gem.util.ConditionUtil;
import com.gem.util.JSONResultUtil;
import com.gem.util.MessageUtil;
import com.gem.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Apolar_GuanYH-管阳红
 * Description: 日常经费控制器
 * create 2019/11/4 11:03
 * Modified By:
 */
@Controller
@RequestMapping("/mds")
public class MobileCDailySpendingController {

    //自动注入ConditionUtil
    private ConditionUtil cu;

    @Autowired
    public void setCu(ConditionUtil cu) {
        this.cu = cu;
    }

    //自动注入UserService
    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    //自动注入CDailySpendingService
    private CDailySpendingService cDailySpendingService;

    @Autowired
    public void setcDailySpendingService(CDailySpendingService cDailySpendingService) {
        this.cDailySpendingService = cDailySpendingService;
    }

    //自动注入JSONResultUtil
    private JSONResultUtil result;

    @Autowired
    public void setResult(JSONResultUtil result) {
        this.result = result;
    }

    @RequestMapping("/dsShow")
    public String dsShow(HttpSession session, Model model, @RequestParam(value = "currentPage", defaultValue = "1") int currentPage, @RequestParam(value = "pageSize", defaultValue = "6") int pageSize) {
        try {
            PageInfo<CDailySpending> pageBean = cDailySpendingService.selectDSByPageM(currentPage, pageSize, session);
            if (pageBean != null && pageBean.getList().size() > 0) {
                model.addAttribute("pageBean", pageBean);
                List<Integer> pageValue = PageUtil.getPageValue(pageBean.getPages(), currentPage);
                model.addAttribute("pageValue", pageValue);
            } else {
                model.addAttribute("info", "暂无信息记录");
            }
            model.addAttribute("pageSize", pageSize);
            return "mobile_training/DailySpending";
        } catch (Exception e) {
            e.printStackTrace();
        }
        MessageUtil.returnInfoPage("3", "/mobile_welcome", "服务器异常，请联系管理员哦！", model);
        return "message";
    }

    //查询显示
    @RequestMapping("/dsSearchShow")
    public String dsSearch(HttpSession session, String cdscDate, String cdscSeller, String cdscPeople, Model model, @RequestParam(value = "currentPage", defaultValue = "1") int currentPage, @RequestParam(value = "pageSize", defaultValue = "6") int pageSize) {
        try {
            List<User> userList = userService.selectAllByPersonName('%' + cdscSeller + '%');
            if (userList.size() > 0) {
                Integer[] sellerUidArr = new Integer[userList.size()];
                for (int i = 0; i < userList.size(); i++) {
                    sellerUidArr[i] = userList.get(i).getUid();
                }
                cu.setCdscSellerArr(sellerUidArr);
            }
            cu.setCdscDate(cdscDate);
            cu.setCdscPeople(cdscPeople);
            if (!(cdscSeller != null && userList.size() == 0)) {
                PageInfo<CDailySpending> pageBean = cDailySpendingService.selectDSByConditionByPageM(currentPage, pageSize, cu, session);
                if (pageBean != null && pageBean.getList().size() > 0 && pageBean.getPages() > 0 && pageBean.getTotal() > 0) {
                    List<Integer> pageValue = PageUtil.getPageValue(pageBean.getPages(), currentPage);
                    model.addAttribute("pageValue", pageValue);
                    model.addAttribute("pageBean", pageBean);
                } else {
                    model.addAttribute("info", "暂无信息记录");
                }
            } else {
                model.addAttribute("info", "暂无信息记录");
            }
            cu.setCdscSeller(cdscSeller);
            model.addAttribute("cdsc", cu);
            model.addAttribute("pageSize", pageSize);
            model.addAttribute("searchURL", "/mds/dsSearchShow?" + "cdscDate=" + cdscDate + "&cdscSeller=" + cdscSeller + "&cdscPeople=" + cdscPeople);
            return "mobile_training/DailySpending";
        } catch (Exception e) {
            e.printStackTrace();
        }
        MessageUtil.returnInfoPage("3", "/mds/dsShow", "服务器异常，请联系管理员哦！", model);
        return "message";
    }

    //修改页面
    @RequestMapping("/dsModifyUI")
    public String dsModifyUI(String dsId, Model model, @RequestParam(value = "currentPage", defaultValue = "1") int currentPage, @RequestParam(value = "pageSize", defaultValue = "6") int pageSize) {
        try {
            CDailySpending cDailySpending = cDailySpendingService.selectDSByIdService(Integer.parseInt(dsId));
            List<User> userList = userService.findAllUser();
            if (cDailySpending != null && userList.size() > 0) {
                model.addAttribute("ds", cDailySpending);
                model.addAttribute("sellerList", userList);
                model.addAttribute("currentPage", currentPage);
                model.addAttribute("pageSize", pageSize);
                return "mobile_training/DailySpending_modify";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        MessageUtil.returnInfoPage("3", "/mds/dsShow", "服务器异常，请联系管理员哦！", model);
        return "message";
    }

    //修改功能
    @RequestMapping(value = "/dsModify", method = RequestMethod.POST)
    @ResponseBody
    public JSONResultUtil dsModify(CDailySpending cDailySpending) {
        try {
            if (cDailySpendingService.modifyDSService(cDailySpending)) {
                result.setId(0);
                result.setData("修改成功！");
            } else {
                result.setId(1);
                result.setData("修改未成功哦，请重试！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setId(-1);
            result.setData("服务器异常，请联系管理员哦！");
        }
        return result;
    }

    //添加页面
    @RequestMapping("/dsAddUI")
    public String dsAddUI(Model model) {
        try {
            List<User> userList = userService.findAllUser();
            if (userList.size() > 0) {
                model.addAttribute("sellerList", userList);
                return "mobile_training/DailySpending_insert";
            } else {
                MessageUtil.returnInfoPage("3", "/mds/dsShow", "暂无渠道专员信息哦！", model);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        MessageUtil.returnInfoPage("3", "/mds/dsShow", "服务器异常，请联系管理员哦！", model);
        return "message";
    }

    //添加功能
    @RequestMapping(value = "/dsAdd", method = RequestMethod.POST)
    @ResponseBody
    public JSONResultUtil cpAdd(CDailySpending cDailySpending, Model model, @RequestParam(value = "currentPage", defaultValue = "1") int currentPage, @RequestParam(value = "pageSize", defaultValue = "6") int pageSize) {
        try {
            if (cDailySpendingService.addDSService(cDailySpending)) {
                result.setId(0);
                result.setData("添加成功！");
            } else {
                result.setId(1);
                result.setData("添加未成功哦，请重试！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setId(-1);
            result.setData("服务器异常，请联系管理员哦！");
        }
        return result;
    }
}
